function toChineseNumber(num) {
  const strs = num
    .toString()
    .replace(/(?=(\d{4})+$)/g, ",")
    .split(",")
    .filter(Boolean);

  const chars = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  const units = ["", "十", "百", "千"];
  const bigUnits = ["", "万", "亿", "兆"];

  function _transform(numStr) {
    let result = "";
    for (let i = 0; i < numStr.length; i++) {
      const digit = Number(numStr[i]);
      const c = chars[digit];
      const u = units[numStr.length - 1 - i];
      if (digit === 0) {
        if (result[result.length - 1] !== chars[0]) {
          result += c;
        }
      } else {
        result += c + u;
      }
    }
    if (result[result.length - 1] === chars[0]) {
      result = result.slice(0, -1);
    }
    return result;
  }

  let result = "";
  for (let i = 0; i < strs.length; i++) {
    const part = strs[i];
    const c = _transform(part);
    if (c !== "") {
      const u = bigUnits[strs.length - 1 - i];
      result += c + u;
    }
  }

  if (result[result.length - 1] === chars[0]) {
    result = result.slice(0, -1);
  }

  return result;
}

console.log(toChineseNumber(4000000000000)); // 输出: 四兆
